//
//  NewsView.m
//  A段项目
//
//  Created by 官琛 on 15-6-19.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "NewsView.h"

@implementation NewsView


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}

-(void)createView
{
    
    [self afnGetRequest1];
    [self afnGetRequest];
    self.scroollView = [[UIScrollView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.scroollView.contentSize = CGSizeMake(0, 375*2+30);
    //    self.scroollView.bounces = NO;
    [self addSubview:self.scroollView];
    //    self.scroollView set

    
    self.MovieBack = [[UIImageView alloc]initWithFrame:CGRectMake(0, 20, 375, 280)];
//    self.MovieBack.backgroundColor = [UIColor lightGrayColor];
    self.MovieBack.image = [UIImage imageNamed:@"News_back.png"];
    [self.scroollView addSubview:self.MovieBack];
    self.MovieBack.userInteractionEnabled = YES;
    [_MovieBack release];
    
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 80, 30)];
    self.titleLabel.text = @"每日佳片";
    self.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    self.titleLabel.textColor = [UIColor whiteColor];
    [self.MovieBack addSubview:self.titleLabel];
    [_titleLabel release];
    
    self.MovieView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 40, 375 , 150)];
//    self.MovieView.backgroundColor = [UIColor orangeColor];
    [self.MovieBack addSubview:self.MovieView];
    self.MovieView.userInteractionEnabled = YES;
    [_MovieView release];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [self.MovieBack addGestureRecognizer:tap];
    
    self.subLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 222, 330, 50)];
//    self.subLabel.backgroundColor = [UIColor whiteColor];
    [self.MovieBack addSubview:self.subLabel];
    self.subLabel.textColor = [UIColor whiteColor];
    self.subLabel.numberOfLines = 0;
    [_subLabel release];
    
    self.MovieLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 200, 330, 20)];
    self.MovieLabel.font = [UIFont boldSystemFontOfSize:19];
    self.MovieLabel.textColor = [UIColor whiteColor];
    self.MovieLabel.text = @"加载中...";
    //    [self.MovieLabel sizeToFit];
//    self.MovieLabel.backgroundColor = [UIColor cyanColor];
    [self.MovieBack addSubview:self.MovieLabel];
    [_MovieBack release];
    
    self.HotBack = [[UIView alloc]initWithFrame:CGRectMake(0, self.MovieBack.frame.origin.y +self.MovieBack.frame.size.height+20, 375, 400)];
    self.HotBack.backgroundColor = [UIColor whiteColor];
    [self.scroollView addSubview:self.HotBack];
    [_HotBack release];
    
    self.HotLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 80, 30)];
    self.HotLabel.text = @"今日热点";
    self.HotLabel.font = [UIFont boldSystemFontOfSize:20];
//    self.HotLabel.textColor = [UIColor whiteColor];
    [self.HotBack addSubview:self.HotLabel];
    [_HotLabel release];
    
    
    self.HotView1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 40, 375, 160)];
    self.HotView1.backgroundColor = [UIColor orangeColor];
     self.HotView1.image = [UIImage imageNamed:@"News_back.png"];
//    self.HotView1.layer.masksToBounds = YES;
//    self.HotView1.layer.cornerRadius =  20;
    self.HotView1.userInteractionEnabled = YES;
    [self.HotBack addSubview:self.HotView1];
    [_HotView1 release];
    
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction1:)];
    [self.HotView1 addGestureRecognizer:tap1];
    

    
    
    self.HotImage1 = [[UIImageView alloc]initWithFrame:CGRectMake(5, 40, 130, 90)];
//    self.HotImage1.backgroundColor = [UIColor blueColor];
    [self.HotView1 addSubview:self.HotImage1];
    [_HotImage1 release];
    
    
    
    
    
    self.title1 = [[UILabel alloc]initWithFrame:CGRectMake(140, 20, 235, 50)];
    self.title1.numberOfLines = 0;
    self.title1.textColor = [UIColor whiteColor];
    self.title1.font = [UIFont boldSystemFontOfSize:20];
//    self.title1.backgroundColor = [UIColor cyanColor];
    [self.HotView1 addSubview:self.title1];
    [_title1 release];
    
    self.imageLabel1 = [[UILabel alloc]initWithFrame:CGRectMake(140, 70, 235, 80)];
    self.imageLabel1.numberOfLines = 0;
    self.imageLabel1.textColor = [UIColor whiteColor];
    self.imageLabel1.font = [UIFont systemFontOfSize:16];
//    self.imageLabel1.backgroundColor = [UIColor whiteColor];
    [self.HotView1 addSubview:self.imageLabel1];
    [_imageLabel1 release];
    
    self.HotView2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, self.HotView1.frame.origin.y+self.HotView1.frame.size.height+20, 375, 160)];
    self.HotView2.backgroundColor = [UIColor orangeColor];
     self.HotView2.image = [UIImage imageNamed:@"News_back.png"];
//    self.HotView2.layer.masksToBounds = YES;
//    self.HotView2.layer.cornerRadius =  20;
    [self.HotBack addSubview:self.HotView2];
    [_HotView2 release];
    
    
    
    
    
    
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction2:)];
    [self.HotView2 addGestureRecognizer:tap2];
    self.HotView2.userInteractionEnabled = YES;
    
    self.HotImage2 = [[UIImageView alloc]initWithFrame:CGRectMake(5, 40, 130, 90)];
//    self.HotImage2.backgroundColor = [UIColor blueColor];
    [self.HotView2 addSubview:self.HotImage2];
    [_HotImage1 release];
    
    self.title2 = [[UILabel alloc]initWithFrame:CGRectMake(140, 20, 235, 50)];
    self.title2.textColor = [UIColor whiteColor];
        self.title2.font = [UIFont boldSystemFontOfSize:20];
//    self.title2.backgroundColor = [UIColor cyanColor];
    self.title2.numberOfLines = 0;
    [self.HotView2 addSubview:self.title2];
    [_title2 release];
    
    self.imageLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(140, 70, 235, 80)];
    self.imageLabel2.textColor = [UIColor whiteColor];
    self.imageLabel2.numberOfLines = 0;
    self.imageLabel2.font = [UIFont systemFontOfSize:16];
//    self.imageLabel2.backgroundColor = [UIColor whiteColor];
    [self.HotView2 addSubview:self.imageLabel2];
    [_imageLabel2 release];
    
    self.HUD = [MBProgressHUD showHUDAddedTo:self.scroollView animated:YES];
    self.HUD.labelText = @"正在加载中";
    [self.HUD show:YES];
    [_HUD autorelease];
    
}
-(void)buttonAction:(UIButton *)button
{
    NSLog(@"111");
    
}


-(void)afnGetRequest
{
    
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    NSString *url_string = @"http://api.m.mtime.cn/PageSubArea/GetFirstPageAdvAndNews.api";
//NSString *url_string = @"http://api.m.mtime.cn/PageSubArea/HotPlayMovies.api?locationId=729";
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"application/x-javascript",@"text/html",nil];
    

    
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
//        NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        NSMutableArray *array = [NSMutableArray array ];
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];
        array = [responseObject objectForKey:@"hotPoints"];
        
        dic = [array objectAtIndex:0];
        dic1 = [array objectAtIndex:1];
        
        NSURL *url1 = [NSURL URLWithString:[dic objectForKey:@"img"]];
        [self.HotImage1 sd_setImageWithURL:url1 placeholderImage:[UIImage imageNamed:@"12346.jpg"]];
        self.imageLabel1.text = [dic objectForKey:@"desc"];
        self.title1.text = [dic objectForKey:@"title"];
        self.newsId1 = [[dic objectForKey:@"id"]integerValue];
        
        NSURL *url2 = [NSURL URLWithString:[dic1 objectForKey:@"img"]];
        [self.HotImage2 sd_setImageWithURL:url2 placeholderImage:[UIImage imageNamed:@"12346.jpg"]];
        self.imageLabel2.text = [dic1 objectForKey:@"desc"];
        self.title2.text = [dic1 objectForKey:@"title"];
        self.newsId2 = [[dic1 objectForKey:@"id"]integerValue];
        
        [self.HUD hide:YES];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
         [self.HUD hide:YES];
    }];
    
}




-(void)afnGetRequest1
{
    
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    NSString *url_string = @"http://api.m.mtime.cn/PageSubArea/GetFirstPageAdvAndNews.api";
 
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"application/x-javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
//        NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];
        dic = [responseObject objectForKey:@"hotMovie"];
        NSURL *url = [NSURL URLWithString:[dic objectForKey:@"topCover"]];
        [self.MovieView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@""]];
        
        self.MovieLabel.text = [dic objectForKey:@"title"];
        
        dic1 = [dic objectForKey:@"movie"];
        self.subLabel.text = [dic1 objectForKey:@"desc"];
        
        self.DailyId = [[dic1 objectForKeyedSubscript:@"movieId"]integerValue];
       
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        
    }];
    
}












//每日佳片的触碰手势
-(void)tapAction:(UITapGestureRecognizer *)button
{
    [self.myDetegate pushDailyMovie:self.DailyId];
    
}
//热点1触碰
-(void)tapAction1:(UITapGestureRecognizer *)button
{
   
//    NSLog(@"11");
    [self.myDetelate pushHotPointOne:self.newsId1];
    
}
//热点2触碰
-(void)tapAction2:(UITapGestureRecognizer *)button
{
//    NSLog(@"55215");
    [self.myDetelate2 pushHotPointTwo:self.newsId2];
}


@end
